//<----树形数据转换---->
function cb(item) {
    let newObj = {}
    newObj.key = item.id
    newObj.value = item.id
    newObj.title = item.name
    if (!!item.children) {
        newObj.children = conversionTree(cb, item.children)
    }
    return newObj
}

function conversionTree(cb, data) {
    return data.map(item => cb(item))
}
//<----树形数据转换---->

//<----树形数据按条件更改---->
function screenfnTree(data, res) {
    return data.map(item => {
        let newObj = {};
        if (res.id === item.id) {
            newObj = res;
            if (item.children) newObj.children = item.children;
        } else {
            newObj = item;
            if (item.children) newObj.children = screenfnTree(item.children, res);
        }
        return newObj
    })
}
//<----树形数据按条件更改---->

//<----树形数据添加数据---->
function pushTree(data, res) {
    return data.map(item => {
        let newObj = {};
        newObj = item;
        if (res.parentId === item.id) {
            if (item.children) {
                newObj.children.push(res)
            } else {
                newObj.children = []
                newObj.children.push(res)
            }
        } else {
            if (item.children) newObj.children = screenfnTree(item.children, res);
        }
        return newObj
    })
}
//<----树形数据添加数据---->

//<----时间戳格式化---->
function formatDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    //  return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
    return year + '-' + (String(month).length > 1 ? month : '0' + month) + '-' +
        (String(day).length > 1 ? day : '0' + day) + ' ' + (String(hour).length > 1 ? hour : '0' + hour) + ':' + (String(minute).length > 1 ? minute : '0' + minute)
        + ':' + (String(second).length > 1 ? second : '0' + second)
}
//<----时间戳格式化---->

//<----菜单格式化为路由，并将其存储为路径地址库，方便做面包屑---->
function menuTransformationRouters(menuData) {
    let newRouters = []
    let pathList = []
    let fatherPath = ''
    function traversalRoute(routes) {
        routes.forEach(Item => {
            let pashTtem = {}
            pashTtem.path = Item.path
            pashTtem.icon = Item.icon
            pashTtem.name = Item.name
            pathList.push(pashTtem)
            if (!Item.children) {
                let routeItem = {}
                routeItem.key = Item.id
                routeItem.name = Item.name
                routeItem.icon = Item.icon
                routeItem.path = fatherPath + Item.path
                routeItem.component = `./pages/modulars${Item.url}`
                newRouters.push(routeItem)
            } else {
                fatherPath = fatherPath + Item.path
                traversalRoute(Item.children)
            }
        })
    }
    traversalRoute(menuData)
    return {
        newRouters,
        pathList
    }
}
//<----菜单格式化为路由，并将其存储为路径地址库，方便做面包屑---->
const dataTree = data => conversionTree(cb, data)

const newScreenfnTree = (data, res) => screenfnTree(data, res)

const newPushTree = (data, res) => pushTree(data, res)

const newFormatDate = time => formatDate(time)

const newMenuTransformationRouters = data => menuTransformationRouters(data)

export {
    dataTree,
    newScreenfnTree,
    newPushTree,
    newFormatDate,
    newMenuTransformationRouters
}